package tags;

import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

public class Calendrier extends TagSupport {

	private static final String months[] = { "Janvier", "Fevrier", "Mars",
			"Avril", "Mai", "Juin", "Juillet", "Aout", "Septembre", "Octobre",
			"Novembre", "Decembre" };

	public int doStartTag() throws JspException {
		HttpServletRequest req = null;
		GregorianCalendar cal = null;
		String cmd = null;
		//String oldcmd= null;

		
		req = (HttpServletRequest) pageContext.getRequest();
		cmd = req.getParameter("date");
		
		if(cmd != null){
			cal = (GregorianCalendar) pageContext.getAttribute("date",PageContext.SESSION_SCOPE);
			if (cal == null) {
				cal = new GregorianCalendar();
				cal.set(Calendar.DATE, 1);
				pageContext.setAttribute("date", cal, PageContext.SESSION_SCOPE);
			}	
		}
		else{
			cal = new GregorianCalendar();
			cal.set(Calendar.DATE, 1);
			pageContext.setAttribute("date", cal, PageContext.SESSION_SCOPE);
		}

		/*oldcmd = req.getParameter("submit");
		
		
		
		//la vielle commande
		if(oldcmd!=null){
			
			if (cmd.equals("<<"))
				cal.add(Calendar.MONTH, -1);
			
			if (cmd.equals(">>"))
				cal.add(Calendar.MONTH, 1);
			
			if (oldcmd.equals("---")) {
				cal = new GregorianCalendar();
				cal.set(Calendar.DATE, 1);
			}
			pageContext.setAttribute("date", cal, PageContext.SESSION_SCOPE);
		}*/
		
		
		//la nouvelle commande
		if (cmd != null) {
			StringTokenizer st = new StringTokenizer(cmd, "\\");
			String firstToken = st.nextToken();
			String secondToken = st.nextToken();
			String thirdToken = st.nextToken();
			int jour = Integer.parseInt(firstToken);
			int mois = Integer.parseInt(secondToken);
			
			if(mois==(cal.get(Calendar.MONTH))){
					cal.add(Calendar.MONTH, -1);
			}

			if(mois==(cal.get(Calendar.MONTH))+2){
				cal.add(Calendar.MONTH, 1);
			}

			pageContext.setAttribute("date", cal, PageContext.SESSION_SCOPE);
		}
		try {
			pageContext.getOut().write(
					"<p><table id='calendar'>");
			generateCalendrierHeader(pageContext.getOut(), cal);
			generateCalendrierTab(pageContext.getOut(), cal);
			pageContext.getOut().write("</table>");
			//pageContext.getOut().write("<table cellpadding='2'>");
			//generateNavigation(pageContext.getOut(), cal);
			//pageContext.getOut().write("</table>");
		} catch (IOException e) {
			throw (new JspException(getClass().getName() + ": " + e.toString()));
		}
		return (SKIP_BODY);
	}

	private void generateCalendrierHeader(JspWriter out, GregorianCalendar cal)
			throws IOException {
		int i = -1;
		int mon = -1;

		out.println("<tr align=\"center\">" 
						+"<th colspan=\"7\" class=\"navigation\">"
						+"<b><a href=\"?date=" + cal.get(Calendar.DATE)
							+ "\\" + (cal.get(Calendar.MONTH)) + "\\"
							+ cal.get(Calendar.YEAR) + "\"> << </a>"
						+ months[cal.get(Calendar.MONTH)]
						+ " "
						+ cal.get(Calendar.YEAR)
						+ "<a href=\"?date=" + cal.get(Calendar.DATE)
							+ "\\" + ((cal.get(Calendar.MONTH))+2) + "\\"
							+ cal.get(Calendar.YEAR) + "\"> >> </a></b>"
						+"</th>"
					+"</tr>");
		
	//acien lien: <a href=\"?submit=>>\"> >> </a>
	}

	private void generateCalendrierTab(JspWriter out, GregorianCalendar cal)
			throws IOException {
		int i = -1;
		int mon = -1;
		int year = -1;

		cal.set(Calendar.DATE, 1);
		out.println("<tr>");
		out.println("<th width=\"14%\" class=\"calendar\">Lun</th>");
		out.println("<th width=\"14%\" class=\"calendar\">Mar</th>");
		out.println("<th width=\"14%\" class=\"calendar\">Mer</th>");
		out.println("<th width=\"14%\" class=\"calendar\">Jeu</th>");
		out.println("<th width=\"14%\" class=\"calendar\">Ven</th>");
		out.println("<th width=\"14%\" class=\"calendar\">Sam</th>");
		out.println("<th width=\"14%\" class=\"calendar\">Dim</th>");
		out.println("</tr>");
		out.println("<tr>");
		for (i = 1; i <= 7; i++) {
			out.print(getHeader(i));
			int day = (cal.get(Calendar.DAY_OF_WEEK) - 1) % 8;
			if (i == day) {
				out.print("<a href=\"?date=" + cal.get(Calendar.DATE) + "\\"
						+ (cal.get(Calendar.MONTH)+1) + "\\"
						+ cal.get(Calendar.YEAR) + "\">"
						+ cal.get(Calendar.DATE) + "</a>");
				cal.add(Calendar.DATE, 1);

			} else {

				if (day == 0&&i==7) {
					out.print("<a href=\"?date=" + cal.get(Calendar.DATE)
							+ "\\" + (cal.get(Calendar.MONTH)+1) + "\\"
							+ cal.get(Calendar.YEAR) + "\">"
							+ cal.get(Calendar.DATE) + "</a>");			
					cal.add(Calendar.DATE, 1);	
				} else {
					out.println(" ");
				}
			}
			out.println("</td>");

		}
		out.println("</tr>");
		mon = cal.get(Calendar.MONTH);
		year = cal.get(Calendar.YEAR);
		while (mon == cal.get(Calendar.MONTH)) {
			out.println("<tr>");
			for (i = 1; i <= 7; i++) {
				out.print(getHeader(i));
				if (mon == cal.get(Calendar.MONTH)) {
					out.print("<a href=\"?date=" + cal.get(Calendar.DATE)
							+ "\\" + (cal.get(Calendar.MONTH)+1) + "\\"
							+ cal.get(Calendar.YEAR) + "\">"
							+ cal.get(Calendar.DATE) + "</a>");
					cal.add(Calendar.DATE, 1);
				} else
					out.println(" ");
				out.println("</td>");
			}
			out.println("</tr>");
		}
		cal.set(Calendar.MONTH, mon);
		if(mon==11)
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.DATE, 1);
	}

	private void generateNavigation(JspWriter out, GregorianCalendar cal) throws IOException {
		//cal = new GregorianCalendar();
		//cal.add(Calendar.MONTH, Calendar.MONTH-1);
		out.println(cal.get(Calendar.DATE)+"/"+cal.get(Calendar.MONTH)+"/"+cal.get(Calendar.YEAR) );
		/*
		out.println("<tr>"
		+"<td class=\"navigation\">"
		+"<a href='?date="
		+ cal.get(Calendar.DATE)+ "\\" 
		+ cal.get(Calendar.MONTH)+ "\\"
		+ cal.get(Calendar.YEAR)+"'>"
		+"<input type='button' value=\"Aujourd'hui\" class='buttonImg'>"
		+"</a></td>"
		+ "</tr>");		
		*/
		
		out.println("<tr>"
				+"<td class=\"navigation\">"
				+"<a href='jour.jsp'>"
				+"<input type='button' value=\"Aujourd'hui\" class='buttonImg'>"
				+"</a></td>"
				+ "</tr>");		

	}

	private String getHeader(int dayOfWeek) {
		String style = "weekday";

		if ((dayOfWeek == 1) || (dayOfWeek == 7))
			style = "weekend";
		return ("<td align='center' class=\"" + style + "\">");
	}
}
